
package com.ifcar.app.util;

import com.ifcar.app.util.GroupAdapter.ItemPosition;
import com.ifcar.app.view.PullRefreshListView;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.AbsListView;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.AbsListView.OnScrollListener;

public class GroupListView extends PullRefreshListView implements OnScrollListener {

    private FrameLayout mTopView;

    // private PullListView mListView;

    @SuppressWarnings("unchecked")
    private GroupAdapter mGroupAdapter;

    private int mCurrentGroup = -1;

    private TopViewCallback mTopViewCallback;

    // private boolean mDrawTop;

    public GroupListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    public GroupListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public GroupListView(Context context) {
        super(context);
        init(context);
    }

    private void init(Context context) {

    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        mTopView = new FrameLayout(getContext());

        FrameLayout.LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT, Gravity.TOP);
        addView(mTopView, lp);

        getListView().setOnScrollListener(this);
    }
    
    

    // public ListView getListView() {
    // return mListView;
    // }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        if (mTopView.getVisibility() != View.GONE) {
            FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) mTopView.getLayoutParams();
            int t = lp.topMargin;
            int b = t + mTopView.getMeasuredHeight();
            mTopView.layout(0, t, getMeasuredWidth(), b);
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        measureChildWithMargins(mTopView, widthMeasureSpec, 0, heightMeasureSpec, 0);
    }

    @SuppressWarnings("unchecked")
    public void setAdapter(GroupAdapter adapter) {
        mGroupAdapter = adapter;
        getListView().setAdapter(adapter);
    }

    public void setTopViewCallback(TopViewCallback callback) {
        mTopViewCallback = callback;
        if (callback != null) {
            mTopView.removeAllViews();
            View top = callback.createView();
            if (top != null) {
                FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) top.getLayoutParams();
                if (lp == null) {
                    lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
                }
                mTopView.addView(top, lp);
            }

        } else {
            mTopView.removeAllViews();
        }
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
            int totalItemCount) {
        if (firstVisibleItem == INVALID_VALUE || mGroupAdapter == null
                || mGroupAdapter.getCount() == 0 || getHeight() == 0) {
            mTopView.setVisibility(GONE);
            return;
        }
        ListView listView = getListView();
        int first = firstVisibleItem;
        for (int i = 0; i < visibleItemCount; i++) {
            if (listView.getChildAt(i).getBottom() > getRefreshHeight()) {
                first += i;
                break;
            }
        }
        
        if (first < listView.getHeaderViewsCount() + 1) {
            mTopView.setVisibility(GONE);
            return;
        }

        first -= (listView.getHeaderViewsCount() + 1);
        firstVisibleItem -= (listView.getHeaderViewsCount() + 1); // 还有个下拉view

        mTopView.setVisibility(VISIBLE);

        ItemPosition itemPosition = mGroupAdapter.getRealPosition(first);

        FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) mTopView.getLayoutParams();
        if (mCurrentGroup != itemPosition.group) {
            mCurrentGroup = itemPosition.group;
            callTopViewUpdate(mCurrentGroup);
            lp.topMargin = 0;
            mTopView.requestLayout();
            // mTopView.setVisibility(VISIBLE);
        } else if (itemPosition.pos >= mGroupAdapter.getChildrenCount(mCurrentGroup) - 1) {
            FrameLayout.LayoutParams l = (FrameLayout.LayoutParams) listView.getLayoutParams();
            int firstBottom = view.getChildAt(first - firstVisibleItem).getBottom()
                    + l.topMargin;
            int topHeight = mTopView.getHeight();

            if (firstBottom < topHeight) {
                lp.topMargin = firstBottom - topHeight;
                mTopView.requestLayout();
            } else if (lp.topMargin != 0) {
                lp.topMargin = 0;
                mTopView.requestLayout();
            }
            // System.out.println(view.getChildAt(0).getTop());
        } else if (lp.topMargin != 0) {
            lp.topMargin = 0;
            mTopView.requestLayout();
        }
        // .setLayoutParams(lp);

    }

    private void callTopViewUpdate(int group) {
        if (mTopViewCallback != null) {
            mTopViewCallback.updateView(mTopView.getChildAt(0), group);
        }
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {

    }

    public interface TopViewCallback {
        public View createView();

        public void updateView(View view, int group);
    }

    // private PullRefreshListView mPullRefreshListView;

    // @Override
    // public boolean onTouch(MotionEvent ev) {
    // return mListView.onTouch(ev);
    // }
    //    
    // @Override
    // public boolean clearTouch(MotionEvent ev) {
    // return mListView.clearTouch(ev);
    // }
    //
    // @Override
    // public boolean dispatchTouchEvent(MotionEvent ev) {
    // return mListView.dispatchTouchEvent(ev);
    // }
    //
    // @Override
    // public void setPullRefreshListView(PullRefreshListView
    // pullRefreshListView) {
    // // mPullRefreshListView = pullRefreshListView;
    // mListView.setPullRefreshListView(pullRefreshListView);
    // }
    //
    // @Override
    // public void setRefreshViewForList(View refreshView) {
    // mListView.setRefreshViewForList(refreshView);
    // }
}
